<?php
require_once("jpgraph/jpgraph.php");
//require_once("jpgraph/jpgraph_line.php");
require_once("jpgraph/jpgraph_bar.php");
require_once("common/commonFns.php");
//connect database
include "connect/connect_dsccmis.php";

$cond_prov_str = $_GET["cond_prov_str"];
$cond_cause_str = $_GET["cond_cause_str"];
$date_start = $_GET["date_start"];
$date_end = $_GET["date_end"];

//à¹€à¸‡à¸·à¹ˆà¸­à¸™à¹„à¸‚à¸ˆà¸±à¸‡à¸«à¸§à¸±à¸”à¹€à¸­à¸²à¹„à¸›à¸”à¸¶à¸‡à¸ˆà¸²à¸�à¸•à¸²à¸£à¸²à¸‡ mis
$prov_name = $cond_prov_str;
$prov_name = str_replace('90','à¸ªà¸‡à¸‚à¸¥à¸²',$prov_name);
$prov_name = str_replace('94','à¸›à¸±à¸•à¸•à¸²à¸™à¸µ',$prov_name);
$prov_name = str_replace('95','à¸¢à¸°à¸¥à¸²',$prov_name);
$prov_name = str_replace('96','à¸™à¸£à¸²à¸˜à¸´à¸§à¸²à¸ª',$prov_name);

$cond_prov_str = " and prov in ".$cond_prov_str; 

//à¹€à¸‡à¸·à¹ˆà¸­à¸™à¹„à¸‚à¸ªà¸²à¹€à¸«à¸•à¸¸à¹€à¸­à¸²à¹„à¸›à¸”à¸¶à¸‡à¸ˆà¸²à¸�à¸•à¸²à¸£à¸²à¸‡ mis
//à¹€à¸‡à¸·à¹ˆà¸­à¸™à¹„à¸‚à¸ªà¸²à¹€à¸«à¸•à¸¸à¹€à¸­à¸²à¹„à¸›à¸”à¸¶à¸‡à¸ˆà¸²à¸�à¸•à¸²à¸£à¸²à¸‡ mis
$cause_name = $cond_cause_str;
$cause_name = str_replace('1','à¸„à¸§à¸²à¸¡à¹„à¸¡à¹ˆà¸ªà¸‡à¸š',$cause_name);
$cause_name = str_replace('2','à¸­à¸¢à¸¹à¹ˆà¸£à¸°à¸«à¸§à¹ˆà¸²à¸‡à¸•à¸£à¸§à¸ˆà¸ªà¸­à¸š',$cause_name);
$cause_name = str_replace('3','à¸­à¸¸à¸šà¸±à¸•à¸´à¹€à¸«à¸•à¸¸',$cause_name);
$cause_name = str_replace('4','à¹€à¸£à¸·à¹ˆà¸­à¸‡à¸ªà¹ˆà¸§à¸™à¸•à¸±à¸§',$cause_name);
$cause_name = str_replace('5','à¸­à¸·à¹ˆà¸™à¹†',$cause_name);
$cause_name = str_replace('9','à¹„à¸¡à¹ˆà¸£à¸°à¸šà¸¸',$cause_name);
$cause_name = str_replace('(','',$cause_name);
$cause_name = str_replace(')','',$cause_name);

$cond_cause_str = " and cdscc in ".$cond_cause_str; 


 //à¸ªà¸£à¹‰à¸²à¹€à¸‡à¸·à¹ˆà¸­à¸™à¹„à¸‚ à¸§à¸±à¸™à¸—à¸µà¹€à¸£à¸´à¹ˆà¸¡à¸•à¹‰à¸™
 if($date_start <> ""){
 	$date_start = str_replace('-','',$date_start);
 	$cond_date_start = " and evndte >= '".$date_start."'";
 }else{
 	$cond_date_start = "";
 }
 //à¸ªà¸£à¹‰à¸²à¸‡à¹€à¸‡à¸·à¹ˆà¸­à¸™à¹„à¸‚ à¸§à¸±à¸™à¸—à¸µà¹ˆà¸ªà¸´à¹‰à¸™à¸ªà¸¸à¸”
 if($date_end <> ""){
 	$date_end = str_replace('-','',$date_end);
  	$cond_date_end = " and evndte <= '".$date_end."'";
 }else{
 	$cond_date_end = "";
 }

/*$sql = "select amp,DISTNME,died,inj,disabled from (select amp,count(case when summlosbody_new ='1' then 1 end) as died,count(case when summlosbody_new='2' then 1 end) as inj,count(case when summlosbody_new='3' then 1 end) as disabled from mis_numvictim_losbody where 1 ".$cond_prov_str.$cond_date_start.$cond_date_end.$cond_cause_str." group by amp) t2 left join codebook_dopa_district t1 on t2.amp=t1.DISTID order by amp";
//echo $sql ;
//$arr_xdata_occid_order = array();
$rs_xdata = mysql_query($sql ) or die(mysql_error());*/
$sql = "select amp,DISTNME,died,inj,disabled from (select amp,count(case when summlosbody_new ='1' then 1 end) as died,count(case when summlosbody_new='2' then 1 end) as inj,count(case when summlosbody_new='3' then 1 end) as disabled from mis_numvictim_losbody where 1 ".$cond_prov_str.$cond_date_start.$cond_date_end.$cond_cause_str." group by amp) t2 left join codebook_dopa_district t1 on t2.amp=t1.DISTID order by amp";
//echo $sql ;
$rs_ydata = mysql_query($sql ) or die(mysql_error());
while($row_ydata = mysql_fetch_array($rs_ydata)){ 
		$numdeath_other=$numdeath_other+$row_ydata[2];
		$numinj_other=$numinj_other+$row_ydata[3];
		$numdisabled_other=$numdisabled_other+$row_ydata[4];
		
		$distnme[]=$row_ydata[1];
		$ydata_death[] = $row_ydata[2];
		$ydata_inj[] = $row_ydata[3];
		$ydata_disabled[] = $row_ydata[4];
}

$graph = new Graph(900,450,'auto');	
$graph->SetScale("textlin");
$graph->SetShadow();

$title="à¹�à¸œà¸™à¸ à¸¹à¸¡à¸´à¹�à¸—à¹ˆà¸‡à¹�à¸ªà¸”à¸‡à¸ˆà¸³à¸™à¸§à¸™à¸œà¸¹à¹‰à¹„à¸”à¹‰à¸£à¸±à¸šà¸œà¸¥à¸�à¸£à¸°à¸—à¸šà¹�à¸¢à¸�à¸•à¸²à¸¡à¸„à¸§à¸²à¸¡à¹€à¸ªà¸µà¸¢à¸«à¸²à¸¢";
$graph->title->Set($title);
//à¸�à¸³à¸«à¸™à¸”à¸›à¸£à¸°à¹€à¸ à¸—à¸‚à¸­à¸‡à¸Ÿà¹‰à¸­à¸™à¸•à¹Œà¹ƒà¸«à¹‰à¸�à¸±à¸šà¹„à¸•à¹€à¸•à¸´à¹‰à¸¥à¸šà¸²à¸�à¸£à¸²à¸Ÿ
$graph->title->SetFont(FF_CORDIA,FS_NORMAL,18);
//à¸�à¸³à¸«à¸™à¸”à¸ªà¸µà¹ƒà¸«à¹‰à¸�à¸±à¸šà¹„à¸•à¹€à¸•à¸´à¹‰à¸¥à¸‚à¸­à¸‡à¸�à¸£à¸²à¸Ÿ
$graph->title->SetColor("blue");
//à¸›à¸£à¸±à¸šà¸£à¸°à¸¢à¸°à¸«à¹ˆà¸²à¸‡à¸‚à¸­à¸‡à¹„à¸•à¹€à¸•à¸´à¹‰à¸¥à¸‚à¸­à¸‡à¸�à¸£à¸²à¸Ÿà¸�à¸±à¸šà¸‚à¸­à¸šà¸ à¸²à¸ž
$graph->title->SetMargin(10);
$subtitle = "(à¸•à¸±à¹‰à¸‡à¹�à¸•à¹ˆà¸§à¸±à¸™à¸—à¸µà¹ˆ ".getDateTimeDMY($date_start)." à¸–à¸¶à¸‡à¸§à¸±à¸™à¸—à¸µà¹ˆà¸µ ".getDateTimeDMY($date_end)." à¹ƒà¸™à¸žà¸·à¹‰à¸™à¸—à¸µà¹ˆ: ".$prov_name .")\n(à¸ªà¸²à¹€à¸«à¸•à¸¸: $cause_name)";
$graph->subtitle->Set($subtitle);
$graph->subtitle->SetFont(FF_CORDIA,FS_NORMAL,14);

$graph->img->SetMargin(80,30,75,80);
$graph->xaxis->SetTickLabels($distnme);
$graph->xaxis->SetFont(FF_CORDIA,FS_NORMAL,12);

/*$mygraph->xaxis->SetTickLabels($arr_xdata);
$mygraph->xaxis->SetFont(FF_CORDIA,FS_NORMAL,12);*/
//à¸�à¸³à¸«à¸™à¸”à¸�à¸²à¸£à¹€à¸­à¸µà¸¢à¸‡
$graph->xaxis->SetLabelAngle(30);
$graph->xaxis->SetLabelMargin(5);
//***-***

$xaxistitle="à¸­à¸³à¹€à¸ à¸­";
$graph->xaxis->title->Set($xaxistitle);
$graph->xaxis->SetTitlemargin(40);
$graph->xaxis->title->SetFont(FF_CORDIA,FS_BOLD,14);

$yaxistitle="à¸ˆà¸³à¸™à¸§à¸™à¸œà¸¹à¹‰à¹„à¸”à¹‰à¸£à¸±à¸šà¸œà¸¥à¸�à¸£à¸°à¸—à¸š (à¸„à¸™)";
$graph->yaxis->title->Set($yaxistitle);
$graph->yaxis->SetTitlemargin(40);
$graph->yaxis->title->SetFont(FF_CORDIA,FS_BOLD,14);

$bplot1 = new BarPlot($ydata_death);
$bplot2 = new BarPlot($ydata_inj);
$bplot3 = new BarPlot($ydata_disabled);

$bplot1->SetFillColor("orange");
$bplot2->SetFillColor("brown");
$bplot3->SetFillColor("darkgreen");

$bplot1->SetShadow();
$bplot2->SetShadow();
$bplot3->SetShadow();

$bplot1->SetLegend("à¹€à¸ªà¸µà¸¢à¸Šà¸µà¸§à¸´à¸•");
$bplot2->SetLegend("à¹„à¸”à¹‰à¸£à¸±à¸šà¸šà¸²à¸”à¹€à¸ˆà¹‡à¸š");
$bplot3->SetLegend("à¸žà¸´à¸�à¸²à¸£");

$graph->legend->SetFont(FF_CORDIA,FS_BOLD,10);

$gbarplot = new GroupBarPlot(array($bplot1,$bplot2,$bplot3));
$gbarplot->SetWidth(0.6);
$graph->Add($gbarplot);


$graph->Stroke();
?>